package ken.app.adapter;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.TreeAllBean;
import ken.app.helper.Helper;
import com.she.olympic.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.she.component.adapter.GenView;
import com.she.component.adapter.GeneralAdapter;
import com.she.component.adapter.iface.IGenView;
import com.she.olympic.TreeActivity;

public class TreeAllAdapter extends GeneralAdapter {
	
	static ArrayList<TreeAllBean> mDataList;
	static Handler mhandler;
	static GenSelectAllListItemView mGenSelectAllView = new GenSelectAllListItemView();
	
	public TreeAllAdapter(Context context, List<?> dataList ,Handler handler) {
		super(context, mGenSelectAllView);
		mDataList = (ArrayList<TreeAllBean>) dataList;
		mhandler = handler;
		this.setData(mDataList);
	}
			
	public static class GenSelectAllListItemView extends GenView{
		LayoutInflater inflater;
		
		@Override
		protected View genBody(Context context, View convertView,
				final Object element, final int position) {
						
			if (inflater == null) {
				inflater = LayoutInflater.from(context);
			}
			final ViewHolder holder;
			if (convertView != null	&& convertView.getTag() instanceof ViewHolder) 
			{
				holder = (ViewHolder) convertView.getTag();
			} else 
			{
				convertView = inflater.inflate(R.layout.treeall_row, null);
				holder = new ViewHolder();
				holder.treeType = (TextView) convertView.findViewById(R.id.tree_type);
				holder.treeName = (TextView) convertView.findViewById(R.id.tree_name);
				holder.totalScore = (TextView) convertView.findViewById(R.id.total_score);
				holder.sportsmanImg = (ImageView) convertView.findViewById(R.id.sportsman_img);
				holder.fruitImg = (ImageView) convertView.findViewById(R.id.fruit_img);
				holder.textSmailText = (TextView) convertView.findViewById(R.id.smailtext);
				holder.todayHourMin = (TextView) convertView.findViewById(R.id.today_hourmin);
				convertView.setTag(holder);
			}
			
			holder.treeName.setText(((TreeAllBean)element).getTreeName());
			holder.treeType.setText(treeTypeToString(((TreeAllBean)element).getTreeType()));

			int TodayScore=((TreeAllBean)element).getTodayScore();
			holder.totalScore.setText(""+((TreeAllBean)element).getTodayScore());
//			Log.d("ken", "TodayScore"+TodayScore);
			/*果實圖*/
			
			if (((TreeAllBean)element).getTodayGet() == 1) {
				holder.fruitImg.setImageResource(R.drawable.fruit_0);
				holder.textSmailText.setVisibility(View.GONE);
				holder.totalScore.setText("今日已採果");
				holder.totalScore.setTextSize(20);
				holder.todayHourMin.setText(Helper.getDateTime("yyyy/MM/dd ") + ((TreeAllBean)element).getTodayhourmin());
			}else{
				holder.textSmailText.setVisibility(View.VISIBLE);
				holder.todayHourMin.setText("");
				holder.totalScore.setTextSize(30);
				if (TodayScore >= 1500 & TodayScore < 3000) {
					holder.fruitImg.setImageResource(R.drawable.fruit_01);
				} 
				else if (TodayScore >= 3000 && TodayScore < 4500) {
					holder.fruitImg.setImageResource(R.drawable.fruit_02);
				}
				else if (TodayScore >= 4500 && TodayScore < 6000) {
					holder.fruitImg.setImageResource(R.drawable.fruit_03);
				}
				else if (TodayScore >= 6000) {
					holder.fruitImg.setImageResource(R.drawable.fruit04_ani);
					final AnimationDrawable anidraw0 = (AnimationDrawable) holder.fruitImg.getDrawable();
					holder.fruitImg.post(anidraw0);
				}else{
					holder.fruitImg.setImageBitmap(null);
				}
			}
			
			/*選手圖*/
			
			try {
				Bitmap temp = Helper.readBitMap(context, Helper.getResId(context, "drawable", "tree_" + ((TreeAllBean)element).getTreeId() +"_1"));
				if (temp != null) {
					holder.sportsmanImg.setImageBitmap(temp);
				}else{
					holder.sportsmanImg.setImageBitmap(Helper.readBitMap(context, R.drawable.tree_def_1));
				}
				temp = null;
			} catch (Exception e) {
				holder.sportsmanImg.setImageBitmap(Helper.readBitMap(context, R.drawable.tree_def_1));
			}
			
			return convertView;
		}
		
		private class ViewHolder {
			TextView treeType;
			TextView textSmailText;
			TextView treeName;
			TextView totalScore;
			TextView todayHourMin;
			ImageView sportsmanImg;
			ImageView fruitImg;
		}
		
		private String treeTypeToString(int type){
			String typeString="";
			switch (type) {
			case 1:
				typeString ="水上運動";
				break;
			case 2:
				typeString ="射箭";
				break;
			case 3:
				typeString ="田徑";
				break;
			case 4:
				typeString ="射擊";
				break;
			case 5:
				typeString ="桌球";
				break;
			case 6:
				typeString ="跆拳道";
				break;
			case 7:
				typeString ="舉重";
				break;
			case 8:
				typeString ="自由車";
				break;
			case 9:
				typeString ="擊劍";
				break;
			case 10:
				typeString ="帆船";
				break;
			case 11:
				typeString ="羽球";
				break;
			case 12:
				typeString ="柔道";
				break;
			case 13:
				typeString ="划船";
				break;
			case 14:
				typeString ="網球";
				break;
			}
			
				return typeString;
		}
	}
	
}
